package com.xue.geo.area;

import com.xue.domain.AreaInfo;
import com.xue.geo.domain.Coordinate;
import com.xue.geo.domain.Point;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;

/**
 * @author xue
 * @create 2022-09-16 17:25
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class CircularAreaJudge extends AreaJudge {

    private GlobalCoordinates point;

    private Double radius;

    private Point pointA;

    private Integer radiusA;


    @Override
    public AreaJudge createArea(AreaInfo areaBase) {
        double lon = BigDecimal.valueOf(areaBase.getLon()).divide(BigDecimal.valueOf(10000000), 20, BigDecimal.ROUND_HALF_UP).add(BigDecimal.valueOf(119)).doubleValue();
        double lat = BigDecimal.valueOf(areaBase.getLat()).divide(BigDecimal.valueOf(10000000), 20, BigDecimal.ROUND_HALF_UP).add(BigDecimal.valueOf(30)).doubleValue();
        radius = BigDecimal.valueOf(areaBase.getLdm()).divide(BigDecimal.valueOf(1000), 20, BigDecimal.ROUND_HALF_UP).doubleValue();
        point = new GlobalCoordinates(lat, lon);
        return this;
    }

    @Override
    public Boolean pointInside(Coordinate coordinate) {
        GlobalCoordinates target = new GlobalCoordinates(coordinate.getLatitude(), coordinate.getLongitude());
        double distance = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, point, target).getEllipsoidalDistance();
        return distance < radius;
    }

}
